package com.lijinjiang.reflect02_constructor;

import org.junit.Test;

import java.lang.reflect.Constructor;

/**
 * @ClassName TestConstructor02
 * @Description TODO
 * @Author Li
 * @Date 2022/9/20 20:26
 * @ModifyDate 2022/9/20 20:26
 * @Version 1.0
 */
public class TestConstructor02 {
    // 1.T newInstance(Object... initargs)
    @Test
    public void testNewInstance() throws Exception {
        // 1.获取类对象
        Class c = Student.class;
        // 2.获取Student(String name)构造器，并以此创建对象
        Constructor c01 = c.getDeclaredConstructor(String.class);
        c01.setAccessible(true); // 暴力反射
        Student stu01 = (Student) c01.newInstance("张三");
        System.out.println(stu01);

        // 3.获取Student(String name, int age)构造器，并以此创建对象
        Constructor c02 = c.getDeclaredConstructor(String.class,int.class);
        Student stu02 = (Student) c02.newInstance("李四",27);
        System.out.println(stu02);
    }
}
